/*-----------------------------
----$Source: Volumes://MATEOR/vais/modules/lds_ts/lds_ts.c$
----$Date: June 30, 2012$
-------------------------------
VAIS Copyright Matthew Caswell, 2012
-----------------------------*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<lstring.h>
#include<vais.h>
#include<lds_ts.h>

void initTimeStamp (timeStamp_t* DS_TS, char* DS_Record){
  FILE* DS_FILE;
  
  debug ("initTimeStamp", "initTimeStamp", "main.c");
  
  char c[DS_MD];
  int n = 0;
  
  DS_FILE = fopen (DS_Record, "r");
  
  while(!(feof (DS_FILE))){
    c[n] = (char)fgetc (DS_FILE);
    (*DS_TS).dataStream[n] = c[n];
	debug_c (c[n], "initTimeStamp", "main.c");
    n++;
  }
  
  fclose (DS_FILE);
}

void sortTimeStamp (char str[DS_MD], char* c[DS_MD], timeStamp_t* DS_TS){
  int n = 0;
  
  c[n] = strtok (str, "\n");
  debug (c[n], "sortTimeStamp", "main.c");
  n++;
  while ((c[n] = strtok (null, "\n"))){
    debug (c[n], "sortTimeStamp", "main.c");
    n++;
  }
  
  (*DS_TS).index = n;
}

void parseTimeStamp (timeStamp_t* DS_TS, int index){
  sscanf (DS_TS->dataSeg[index], "PP%6c%4cD%3c%3c%3c", DS_TS->DS_datestamp[index], DS_TS->DS_timestamp[index], DS_TS->DS_lo[index], DS_TS->DS_m[index], DS_TS->DS_hi[index]);
  debug ("PP 6c 4c D 3c 3c 3c", "parseTimeStamp", "main.c");
}

void h_TimeStamp (timeStamp_t* DS_TS, char DS_Record[DS_MD]){
  initTimeStamp (DS_TS, DS_Record);
  sortTimeStamp (DS_TS->dataStream, DS_TS->dataSeg, DS_TS);
  
  int n = 0;
  for(; n < (*DS_TS).index; n++){
    parseTimeStamp (DS_TS, n);
  }
}